﻿#region Using directives

using System;
using System.Collections.Generic;
using System.Text;

#endregion

namespace MetaHeuristics
{
    /// <summary>
    /// A simple timer
    /// </summary>
    public class TimeCounter
    {
        private DateTime startTime = DateTime.Now;
        private DateTime stopTime = DateTime.Now;
        private bool running = false;

        /// <summary>
        /// Gets the start time
        /// </summary>
        /// <value>
        /// A <see cref="DateTime"/> instance representing the start time.
        /// </value>
        public DateTime StartTime
        {
            get
            {
                return this.startTime;
            }
        }

        /// <summary>
        /// Gets the stop time
        /// </summary>
        /// <value>
        /// A <see cref="DateTime"/> instance representing the stop time.
        /// </value>
        public DateTime StopTime
        {
            get
            {
                return this.stopTime;
            }
        }

        /// <summary>
        /// Gets a value indicating if the timer is running
        /// </summary>
        /// <value>
        /// A value indicating if the timer is running.
        /// </value>
        public bool Running
        {
            get
            {
                return this.running;
            }
        }

        /// <summary>
        /// Starts the timer.
        /// </summary>
        public void Start()
        {
            this.startTime = this.stopTime = DateTime.Now;
            this.running = true;
        }

        /// <summary>
        /// Stops the timer.
        /// </summary>
        public void Stop()
        {
            this.stopTime = DateTime.Now;
            this.running = false;
        }

        /// <summary>
        /// Gets the current duration value.
        /// </summary>
        /// <value>
        /// A <see cref="TimeSpan"/> instance representing the current
        /// timer duration.
        /// </value>
        public TimeSpan Duration
        {
            get
            {
                if (running)
                    return DateTime.Now - this.StartTime;
                else
                    return this.StopTime - this.StartTime;
            }
        }
    }
}
